// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async indexAction(){
		//取出菜单项
		this.meta_title = '菜单设置';
		return this.display();
	}
	
	
	async addAction(){
		if(this.isPost){
			let data = this.post();
			//判断数据
			if(!data.name){
				return this.fail('请填写完整数据！');
			}
			let add = await this.model('menu').add(data);
			if(add){
				return this.success({info:"添加成功！"});
			}else{
				return this.fail('添加失败！');
			}	
		}
		let mlist = await this.model('menu').field('id,name').select();
		console.log(mlist);
		this.assign('mlist',mlist);
		return this.display();
	}
	
	async editAction(){
		if(this.isPost){
			let data = this.post();
			//判断数据
			if(!data.name){
				return this.fail('请填写完整数据！');
			}
			let is_save = await this.model('menu').where({id:data.id}).update(data);
			if(is_save){
				return this.success({info:"编辑成功！"});
			}else{
				return this.fail('编辑失败！');
			}
		}else{
			let id = this.get('id');
			let row = await this.model('menu').where({id:id}).find();
			this.assign('row',row);
			//获取菜单数据
			let mlist = await this.model('menu').field('id,name').where({status:1}).select();
			this.assign('mlist',mlist);
			return this.display();
		}	
		
	}
	
	async deleteAction(){
		let id = this.post('id');
		//判断是否有下级
		let child = await this.model('menu').where({pid:id}).find();
		if(!think.isEmpty(child)){
			return this.fail('请先删除当前菜单下子菜单！');
		}
		
		//开始删除
		let is_del = await this.model('menu').where({id:id}).delete();
		
		if(is_del){
			return this.success({info:'删除成功！'});
		}else{
			return this.fail('删除失败！');
		}
	}
	
	
	
	
	
	async getlistAction(){
        let pid = this.get("pid")||0;
        let draw = this.get("draw");
        if(pid){
        let data = await this.model('menu').where({id:pid}).find();
        }
        let i = pid;
        //
        let breadcrumb = []
        while (i!=0)
        {
            let nav = await this.model('menu').where({id:i}).field("id,name,pid").find();
            breadcrumb.push(nav);
            i = nav.pid;

        }
        let all_menu = await this.model('menu').field('id,name').select();
        //all_men
        let obj = {};
          all_menu.forEach(v=>{
              obj[v.id]=v.name;
          })

        let map = {};
        map.pid = pid;
        let list = await this.model('menu').where(map).order("sort asc ,id asc").select();
        //list
        if(list){
            list.forEach((v,k)=>{
                if(v.pid){
                    v.up_title=obj[v.pid];
                }else{
                    v.up_title="一级菜单";
                }
                //console.log(this.setup.MEUN_GROUP)
                //v.group=this.setup.MENU_GROUP[v.group];
            })
        }
        let relist = {
            "draw": draw,
            "data": list,
            "breadcrumb":breadcrumb.reverse()
        }
        //console.log(data);
        return this.json(relist);

    }
  
}